package com.amazon.shop.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.amazon.shop.bean.dto.BaseGroup;
import com.amazon.shop.bean.dto.StoreForm;
import com.amazon.shop.bean.entity.RelUserStore;
import com.amazon.shop.bean.entity.Store;
import com.amazon.shop.bean.entity.User;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.bean.vo.PageVO;
import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.service.IEvalformService;
import com.amazon.shop.service.IPriductService;
import com.amazon.shop.service.IRelUserStoreService;
import com.amazon.shop.service.IStoreService;
import com.amazon.shop.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzj
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private IStoreService tblStoreService;

    @Autowired
    private IRelUserStoreService relUserStoreService;

    @Autowired
    private IPriductService priductService;

    @Autowired
    private IEvalformService esvalformService;

    @RequestMapping("/getAll")
    public Ret getAll(HttpServletRequest request){

        QueryWrapper queryWrapper = new QueryWrapper();
        User user = (User) request.getSession().getAttribute("user");
        if(user!=null){
            List<Integer> storeids = null;
            if (user.getId()!=null) {
                QueryWrapper<RelUserStore> userq = new QueryWrapper();
                userq.select("storeid");
                Integer ismanager = user.getIsmanager();
                if(ismanager==0){
                    userq.eq("userid",user.getId());
                }
                List<RelUserStore> list = relUserStoreService.list(userq);
                storeids = list.stream().map(RelUserStore::getStoreid).collect(Collectors.toList());
            }
            if(storeids!=null&&storeids.size() > 0){
                queryWrapper.in("id",storeids);
                queryWrapper.select("storename","id");
                return ResultUtil.success(tblStoreService.listMaps(queryWrapper));
            }
        }
        return ResultUtil.success(new ArrayList<>());
    }

    @GetMapping("/get")
    public Ret get(@Validated(value = BaseGroup.get.class) StoreForm storeForm){
        return tblStoreService.get(storeForm);
    }

    @GetMapping("/outGet")
    public Ret outGet(String mid){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(mid)) {
            queryWrapper.eq("mid",mid);
            return ResultUtil.success(tblStoreService.list(queryWrapper));
        }
        return ResultUtil.success(new ArrayList<>());
    }

    @PostMapping("/add")
    public Ret add(@Validated(value = BaseGroup.add.class) StoreForm storeForm){
        return tblStoreService.add(storeForm);
    }
    @PostMapping("/update")
    public Ret update(@Validated(value = BaseGroup.update.class) StoreForm storeForm){
        UpdateWrapper<Store> updateWrapper = new UpdateWrapper<> ();
        updateWrapper.eq("id",storeForm.getId());
        if (StrUtil.isNotBlank(storeForm.getStorename())) {
            updateWrapper.set("storename",storeForm.getStorename());
        }
        if (StrUtil.isNotBlank(storeForm.getStoremsg())) {
            updateWrapper.set("storemsg",storeForm.getStoremsg());
        }
        if (StrUtil.isNotBlank(storeForm.getEmail())) {
            updateWrapper.set("email",storeForm.getEmail());
        }
        if (StrUtil.isNotBlank(storeForm.getEmailhost())) {
            updateWrapper.set("emailhost",storeForm.getEmailhost());
        }
        if (StrUtil.isNotBlank(storeForm.getEmailpwd())) {
            updateWrapper.set("emailpwd",storeForm.getEmailpwd());
        }
        if (storeForm.getEmailprot()!=null) {
            updateWrapper.set("emailprot",storeForm.getEmailprot());
        }
        if (StrUtil.isNotBlank(storeForm.getMid())) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("mid",storeForm.getMid());
            queryWrapper.ne("id",storeForm.getId());
            int count = tblStoreService.count(queryWrapper);
            if(count>0){
                return ResultUtil.error(ResultEnum.EXISTERROR.getCode(),ResultEnum.EXISTERROR.getCode(),"店铺网页识别码已存在，请换一个设置");
            }
            updateWrapper.set("mid",storeForm.getMid());
        }
        boolean b = tblStoreService.update(updateWrapper);
        return b?ResultUtil.success():ResultUtil.error(ResultEnum.UPDATEERROR);
    }

    @GetMapping("/del")
    @Transactional
    public Ret delte(@Validated(value = BaseGroup.del.class) StoreForm storeForm){
        boolean remove1 = tblStoreService.removeById(storeForm.getId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("storeid",storeForm.getId());
        Integer userid = Integer.valueOf(storeForm.getOperatorUserid());
        queryWrapper.eq("userid",userid);
        boolean remove2 = relUserStoreService.remove(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("storeid",storeForm.getId());
        boolean remove3 = priductService.remove(queryWrapper);
        if(!remove1||!remove2||!remove3){
            return ResultUtil.error(ResultEnum.DELERROR);
        }
        return ResultUtil.success();
    }
}
